﻿#include "histogramview.h"
#include<QPainter>
#include<QMouseEvent>
HistogramView::HistogramView(QWidget *parent):QAbstractItemView(parent){}
void HistogramView::paintEvent(QPaintEvent *)
{
    QPainter painter(viewport());//viewport作为绘图设备
    painter.setPen(Qt::black);
    int x0=40;
    int y0=250;
    /*完成x,y坐标轴的绘制，并标注坐标轴的变量*/
    //y坐标轴
    painter.drawLine(x0,y0,40,30);
    painter.drawLine(38,32,40,30);
    painter.drawLine(40,30,42,32);
    painter.drawText(20,30,tr("人数"));
    for (int  i= 0; i < 5;i++)//绘制y坐标轴的刻度
    {
        painter.drawLine(-1,-i*50,1,-i*50);
        painter.drawText(-20,-i*50,tr("%1").arg(i*5));
    }
    //x坐标轴
    painter.drawLine(x0,y0,540,250);
    painter.drawLine(538,248,540,250);
    painter.drawLine(540,250,538,252);
    painter.drawText(545,250,tr("部门"));
    int posD=x0+20;
    int row;
    for(row=0;row<model()->rowCount(rootIndex());row++)
    {
        QModelIndex index=model()->index(row,0,rootIndex());
        QString dep=model()->data(index).toString();
        painter.drawText(posD,y0+20,dep);
        posD+=50;
    }
    /*完成表格第一列数据的柱状统计图的绘制*/
    //男
    int posM=x0+20;
    MRegionList.clear();
    for(row=0;row<model()->rowCount(rootIndex());row++)
    {
        QModelIndex index=model()->index(row,1,rootIndex());
        int male=model()->data(index).toDouble();
        int width=10;
        if(selections->isSelected(index))
        {
            painter.setBrush(QBrush(Qt::blue,Qt::Dense3Pattern));
        }else
            painter.setBrush(Qt::blue);
        painter.drawRect(QRect(posM,y0-male*10,width,male*10));//左顶点为(posm,y0-male*10),宽度width,长度male*10
        QRegion regionM(posM,y0-male*10,width,male*10);
        MRegionList.insert(row,regionM);
        posM+=50;
    }
    //女
    int posF=x0+30;
    FRegionList.clear();
    for(row=0;row<model()->rowCount(rootIndex());row++)
    {
        QModelIndex index=model()->index(row,2,rootIndex());
        int female=model()->data(index).toDouble();
        int width=10;
        if(selections->isSelected(index))
        {
            painter.setBrush(QBrush(Qt::red,Qt::Dense3Pattern));
        }else
            painter.setBrush(Qt::red);
        painter.drawRect(QRect(posF,y0-female*10,width,female*10));//左顶点为(posm,y0-male*10),宽度width,长度male*10
        QRegion regionF(posF,y0-female*10,width,female*10);
        FRegionList.insert(row,regionF);
        posF+=50;
    }
    //退休
    int posS=x0+40;
    SRegionList.clear();
    for(row=0;row<model()->rowCount(rootIndex());row++)
    {
        QModelIndex index=model()->index(row,3,rootIndex());
        int retire=model()->data(index).toDouble();
        int width=10;
        if(selections->isSelected(index))
        {
            painter.setBrush(QBrush(Qt::green,Qt::Dense3Pattern));
        }else
            painter.setBrush(Qt::green);
        painter.drawRect(QRect(posS,y0-retire*10,width,retire*10));//左顶点为(posm,y0-male*10),宽度width,长度male*10
        QRegion regionS(posS,y0-retire*10,width,retire*10);
        SRegionList.insert(row,regionS);
        posS+=50;
    }
}
void HistogramView::dataChanged(const QModelIndex &topleft,const QModelIndex &bottomRight)
{
    QAbstractItemView::dataChanged(topleft,bottomRight);//更新
    viewport()->update();
}
void HistogramView::setSelectionModel(QItemSelectionModel *selectionModel)
{
    selections=selectionModel;
}
void HistogramView::selectionChanged(const QItemSelection &selected,const QItemSelection &deselected)
{
    viewport()->update();
}
void HistogramView::mousePressEvent(QMouseEvent *event)
{
    QAbstractItemView::mouseMoveEvent(event);
    setSelection(QRect(event->pos().x(),event->pos().y(),1,1),QItemSelectionModel::SelectCurrent);
}
void HistogramView::setSelection(const QRect &rect,QItemSelectionModel::SelectionFlags flags)
{
    int rows=model()->rowCount(rootIndex());
    int columns=model()->columnCount(rootIndex());
    QModelIndex selectedIndex;
    for(int row=0;row<rows;++row)
    {
        for (int column = 1; column <columns; ++column)
        {
            QModelIndex index=model()->index(row,column,rootIndex());
            QRegion region=itemRegion(index);
            if(!region.intersected(rect).isEmpty())//，它检查这个区域是否与给定的rect相交，如果相交，则将当前索引标记为选定的索引 selectedIndex。
                selectedIndex=index;
        }
        if(selectedIndex.isValid())
        {
            selections->select(selectedIndex,flags);
        }else {
            QModelIndex noIndex;
            selections->select(noIndex,flags);
        }
    }
}

QModelIndex HistogramView::indexAt(const QPoint &point)const//给定的坐标点 point 找到对应的模型索引 QModelIndex 的
{
    QPoint newPoint(point.x(),point.y());
    QRegion region;
    //男
    foreach (region, MRegionList)
    {
        if(region.contains(newPoint))
        {
            int row=MRegionList.indexOf(region);
            QModelIndex index=model()->index(row,1,rootIndex());
            return index;
        }
    }
    //女
    foreach (region, FRegionList)
    {
        if(region.contains(newPoint))
        {
            int row=FRegionList.indexOf(region);
            QModelIndex index=model()->index(row,2,rootIndex());
            return index;
        }
    }
    //退休
    foreach (region, SRegionList)
    {
        if(region.contains(newPoint))
        {
            int row=SRegionList.indexOf(region);
            QModelIndex index=model()->index(row,1,rootIndex());
            return index;
        }
    }
    return QModelIndex();
}
QRegion HistogramView::itemRegion(QModelIndex index)//根据给定的模型索引 index，返回与该索引对应的区域 QRegion
{
    QRegion region;
    if(index.column()==1)
        region=MRegionList[index.row()];//男
    if(index.column()==2)
        region=FRegionList[index.row()];//女
    if(index.column()==3)
        region=SRegionList[index.row()];//退休
    return region;
}

/*以下函数未使用*/
QModelIndex HistogramView::moveCursor(QAbstractItemView::CursorAction cursorAction,Qt::KeyboardModifiers modifiers){}
int HistogramView::horizontalOffset()const{}
int HistogramView::verticalOffset()const{}
bool HistogramView::isIndexHidden(const QModelIndex &index)const{}
QRegion HistogramView::visualRegionForSelection(const QItemSelection &selection)const{}
QRect HistogramView::visualRect(const QModelIndex &index)const{}
void HistogramView::scrollTo(const QModelIndex &index,ScrollHint hint){}
